//
//  DUMediator.m
//  APP
//
//  Created by duyujin1 on 2021/8/23.
//

#import "DUMediator.h"
#import <Foundation/Foundation.h>

@implementation DUMediator

+(__kindof UIViewController *)detailViewControllerWithUrl:(NSString * )detailUrl{
    Class detailClass=NSClassFromString(@"DUDetailViewController");
    UIViewController * controller=[[detailClass alloc] performSelector:NSSelectorFromString(@"initWithUrlString:") withObject:detailUrl];
    return controller;
}

+(NSMutableDictionary *)mediatorCache{
    static NSMutableDictionary *cache;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        cache=@{}.mutableCopy;
    });
    return cache;
}
+(void)registerScheme:(NSString *)scheme processBlock:(DUMediatorPeocessBlock)processBlock{
    if(scheme && processBlock){
        [[[self class] mediatorCache] setObject:processBlock forKey:scheme];
    }
}
+(void)openUrl:(NSString *)url pramas:(NSDictionary *)pramas{
    DUMediatorPeocessBlock block=[[[self class] mediatorCache] objectForKey:url];
    if(block){
        block(pramas);
    }
}

+(void)registerProtocol:(Protocol *)proto class:(Class)cls{
    if(proto && cls){
        [[[self class] mediatorCache] setObject:cls forKey:NSStringFromProtocol(proto)];
    }
}
+(Class)classForPortocol:(Protocol *)proto{
    return [[[self class] mediatorCache] objectForKey:NSStringFromProtocol(proto)];
}

@end
